package com.baseDemo.web.service.navigation.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.baseDemo.web.dao.navigation.NavigationEntityDao;
import com.baseDemo.web.entity.AuthorityEntity;
import com.baseDemo.web.entity.NavigationEntity;
import com.baseDemo.web.entity.UserEntity;
import com.baseDemo.web.server.SystemServer;
import com.baseDemo.web.service.navigation.NavigationService;
import com.share.service.baseServiceSupport.impl.BaseServiceSupport;
@Service
public class NavigationServiceImpl extends BaseServiceSupport<NavigationEntity> implements NavigationService{
	
	@Resource NavigationEntityDao navigationEntityDao;

	@Override
	public NavigationEntity getNavigationEntityById(String id) {
		NavigationEntity navigation = navigationEntityDao.getById(id);
		navigation.setChildren(this.getNavigationEntityByParent(id));
		return navigation;
	}

	@Override
	public List<NavigationEntity> getNavigationEntityByParent(String parent) {
		List<NavigationEntity> list = navigationEntityDao.findByParentId(parent);
		if(list!=null&&list.size()>0){
			for (NavigationEntity navigationEntity : list) {
				if(!navigationEntity.isLeaf()){
					List<NavigationEntity> children = getNavigationEntityByParent(navigationEntity.getId());
					if(children!=null&&children.size()>0){
						navigationEntity.setChildren(children);
					}
				}
			}
		}
		return list;
	}

	@Override
	public String update(List<NavigationEntity> navigationEntities) {
		if(!navigationEntities.isEmpty()){
			for (NavigationEntity navigationEntity : navigationEntities) {
				navigationEntityDao.update(navigationEntity);
			}
			return String.valueOf(1);
		}else{
			return String.valueOf(0);
		}
	}

	@Override
	public String updateNav(NavigationEntity navigationEntity) {
		return navigationEntityDao.updateNav(navigationEntity);
	}

	@Override
	public NavigationEntity getNavigationMenu(String nodeid,HttpServletRequest request) {
		//获取根节点
		NavigationEntity root = navigationEntityDao.getById(nodeid);
		Object object= request.getSession().getAttribute(SystemServer.getInstance().get("USERSESSION").toString());
		if (object!=null) {
			UserEntity user = (UserEntity) object;
			//获取用户权限
			Set<AuthorityEntity> authorities = user.getAuthorities();
			if(authorities!=null&&authorities.size()>0){
				List<String> authorityidlist = new ArrayList<String>();
				for (AuthorityEntity authority : authorities) {
					authorityidlist.add(authority.getId());
				}
				root.setChildren(this.getChildrenByAuth(root.getId(), authorityidlist));
			}
		}
		return root;
	}
	
	private List<NavigationEntity> getChildrenByAuth(String parent,List<String> authorityidlist) {
		List<NavigationEntity> list = navigationEntityDao.getChildrenByAuth(parent,authorityidlist);
		if(list!=null&&list.size()>0){
			for (NavigationEntity navigationEntity : list) {
				if(!navigationEntity.isLeaf()){
					List<NavigationEntity> children = getChildrenByAuth(navigationEntity.getId(),authorityidlist);
					if(children!=null&&children.size()>0){
						navigationEntity.setChildren(children);
					}
				}else{
				}
			}
		}
		return list;
	}

}
